/*
 * Copyright 2008-2009 Daniel Freitas
 *
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Army Creator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Army Creator.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.armycreator;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.wicket.protocol.http.WicketServlet;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

import com.googlecode.armycreator.server.ArmyCreatorServer;
import com.googlecode.armycreator.server.ShutdownMonitor;
import com.googlecode.armycreator.server.SocketServerShutdownMonitor;
import com.googlecode.armycreator.server.wrapper.ArmyCreatorJettyServer;
import com.googlecode.armycreator.util.AwtBrowserWrapper;
import com.googlecode.armycreator.util.BrowserWrapper;

public class StartApplicationHook {
    private static final int DEFAULT_SERVER_PORT = 3379;
    private ArmyCreatorServer server;
    private ShutdownMonitor shutdownMonitor;
    private BrowserWrapper browserWrapper;

    public StartApplicationHook(ArmyCreatorServer server, ShutdownMonitor shutdownMonitor, BrowserWrapper browserWrapper) {
        this.server = server;
        this.shutdownMonitor = shutdownMonitor;
        this.browserWrapper = browserWrapper;
    }

    public static void main(String[] args) throws Exception {
        Server server = new Server(DEFAULT_SERVER_PORT);
        Context context = new Context(server, "/ac", Context.SESSIONS);

        ServletHolder servletHolder = new ServletHolder(new WicketServlet());
        servletHolder.setInitParameter("applicationClassName", "com.googlecode.armycreator.ArmyCreatorApplication");
        servletHolder.setInitOrder(1);
        context.addServlet(servletHolder, "/*");

        ArmyCreatorJettyServer armyCreatorJettyServer = new ArmyCreatorJettyServer(server);
        SocketServerShutdownMonitor jettyShutdownMonitor = new SocketServerShutdownMonitor(armyCreatorJettyServer);
        AwtBrowserWrapper awtBrowserWrapper = new AwtBrowserWrapper();

        StartApplicationHook hook = new StartApplicationHook(armyCreatorJettyServer, jettyShutdownMonitor,
                awtBrowserWrapper);
        hook.startServerInfrastructure();

        hook.openUi();
    }

    /**
     * Starts the server and shutdown monitor.
     * 
     * @throws Exception
     *             If unable to start one of the services.
     */
    public void startServerInfrastructure() throws Exception {
        server.start();
        shutdownMonitor.startMonitoring();
    }

    /**
     * Opens the entry point user UI for Army Creator.
     */
    public void openUi() {
        String url = "http://localhost:" + DEFAULT_SERVER_PORT + "/ac";

        try {
            if (browserWrapper.supportsBrowsing()) {
                browserWrapper.openArmyCreatorUrl(new URL(url));
            }
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException("Unable to open the browser at the requested URL: '" + url + "'", e);
        }
    }
}
